// pages/strategy/strategy.js
var postData = require('../../data/post-data.js')
var app = getApp();
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    winWidth: 0,
    winHeight: 0,
    // tab切换 
    currentTab: 103,
    scrollLeft: 0, //tab标题的滚动条位置
    clicked: -1,
    page: 1,
    cate_id: '0',
    show_up: "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    var that = this
    that.getnavlist()
    that.getlist()
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          postlist: postData.poststrategy,
          the_before: postData.the_before,
          the_loading: postData.the_loading,
          the_after: postData.the_after,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      },

    });
  },
  //导航头部列表
  getnavlist: function(e) {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getconfig'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polists = res.data.datas
        console.log(res.data)
        that.setData({
          polistnav: polists
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  //数据列表
  getlist: function(e) {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.datas
        console.log(res.data)
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  binddetail: function(e) {
    var arcleId = e.currentTarget.dataset.arcleid;
    var detail = 1
    wx.navigateTo({
      url: "../detail/strategy_detail?id=" + [detail, arcleId]
    })
    console.log(e);
  },
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    console.log(1231)
  },

  choose: function(res) {
    var that = this;
    that.setData({
      clicked: res.currentTarget.id,
    })
    console.log(this.data.clicked)
  },
  //导航下拉选择
  choose_up: function(e) {
    var that = this
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/zhankai.png"
    that.setData({
      show_up: show_down,
      show_swiper: true,
      showMask: true
    })
  },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    var that = this;
    var page = 1
    var id = e.target.dataset.current
    var chid = e.target.dataset.chid
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
    if (id > 105) {
      var left = id - 107
      that.setData({
        scrollLeft: left * 100
      })
    } else {
      that.setData({
        scrollLeft: 0
      })
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        clicked: e.currentTarget.id,
        show_up: show_down,
        show_swiper: false,
        showMask: false
      })
    }
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      data: {
        cate_id: id,
        page: page
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.datas
        console.log(res.data)
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      page: page,
      cate_id: id
    })
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  //点击蒙版隐藏
  clickmask: function() {
    var that = this
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
    that.setData({
      show_up: show_down,
      show_swiper: false,
      showMask: false
    })
  },
  //上拉加载
  onReachBottom: function() {
    var that = this
    var id = that.data.cate_id
    console.log(that.data)
    that.setData({
      page: that.data.page + 1 //后台数据需要分页，每次加载一页
    })
    wx.showLoading({ //显示toast
      title: '加载中...',
    });
    var page = that.data.page

    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        cate_id: id,
        page: that.data.page
      },
      method: 'GET',
      success: function(res) {
        if (res.data.code == 200) {
          var polist = that.data.postlist
          for (var i = 0; i < res.data.datas.length; i++) {
            polist.push(res.data.datas[i]);
          }
          console.log(polist)
          that.setData({
            postlist: polist
          });
          //page++

        } else {
          wx.showLoading({
            title: '加载完毕',
          })
        }
        // var listdata = that.data.postlists
        // for (var i = 0; i <= res.data.data.length; i++) {
        //   listdata.push(res.data.data[i]);
        // }
        // that.setData({
        //   postlists: listdata
        // });
        // if (res.data.data.length>listdata.length){
        //   page++
        // }else{
        //   page
        // }


        // if (res.header["X-Pagination-Page-Count"] == res.header["X-Pagination-Current-Page"]){
        //   hadLastpage = res.header["X-Pagination-Current-Page"]
        // }else{
        //   page++;
        // }
        console.log(page)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        wx.hideLoading(); //隐藏toast
        // setTimeout(function() {
        //   wx.showToast({
        //     title: '没有更多数据了！',
        //   })
        // }, 2000)
      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function() {
    var that = this
    var page = that.data.page
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    // wx.showLoading({
    //   title: '数据加载中',
    // })
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {

        page: page
      },
      success: function(res) {
        var polist = res.data.datas
        that.setData({
          postlist: polist
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        setTimeout(function() {
          wx.hideLoading()
        }, 2000)
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

})